package validate

import (
	"regexp"

	"github.com/go-playground/validator/v10"

	"project/study-go/logger"
	"project/study-go/model"
)

//用户校验器 用于注册校验标签和方法
type UserVaildator struct{}

//RegisterValidation user相关校验的注册
func (u *UserVaildator) RegisterValidation() {
	//struct level
	Validator.RegisterStructValidation(userRegistryComparePwd, model.UserRegistry{})
	Validator.RegisterStructValidation(userChgpwd, model.ChangePassword{})

	//field level
	err := Validator.RegisterValidation("registerUsername", registerUsername)
	if err != nil {
		logger.UnexpectLogger.Fatal("register fieldlevel validation error: %s", err)
	}
	Validator.RegisterValidation("registerPassword", registerPassword)
	if err != nil {
		logger.UnexpectLogger.Fatal("register fieldlevel validation error: %s", err)
	}
}

//RegisterTranslation user相关的自定义标签翻译的注册
func (u *UserVaildator) RegisterTranslation() {
	err := Validator.RegisterTranslation("registerUsername", Trans, registerTranslator(
		"registerUsername", "用户名不符合规范"), translate)
	if err != nil {
		logger.UnexpectLogger.Fatal("register translation error: %s", err)
	}
	Validator.RegisterTranslation("registerPassword", Trans, registerTranslator(
		"registerPassword", "密码不符合规范"), translate)
	if err != nil {
		if err != nil {
			logger.UnexpectLogger.Fatal("register translation error: %s", err)
		}
	}
}

/*************************************************/
/*********** structlevel 结构体级别的校验 ***********/
/*************************************************/

//userRegistryComparePwd 注册用户时，比较两次密码是否一致
func userRegistryComparePwd(sl validator.StructLevel) {
	userReg := sl.Current().Interface().(model.UserRegistry)
	if userReg.Password != userReg.RePassword {
		sl.ReportError(userReg.RePassword, "re_password", "RePassword",
			"eqfield", "password")
	}
}

//userChgpwd 用户更改密码时，对新旧密码校验
func userChgpwd(sl validator.StructLevel) {
	chgpwd := sl.Current().Interface().(model.ChangePassword)
	if chgpwd.Newpassword != chgpwd.ReNewpassword {
		sl.ReportError(chgpwd.Newpassword, "newpassword", "NewPassword",
			"eqfield", "re_newpassword")
	}
}

/****************************************************/
/*********** fieldlevel 结构体字段级别的校验 ***********/
/****************************************************/

//registerUsername 注册用户名校验规则
func registerUsername(fl validator.FieldLevel) bool {
	//用户名必须由8到30位连续数字、字母组成
	username := fl.Field().Interface().(string)
	return validateUsername(username)
}

//ValidateUsername 根据用户名校验规则对用户名校验
func validateUsername(username string) bool {
	if len(username) > 30 || len(username) < 8 {
		return false
	}
	str := "^[0-9a-zA-Z]{8,30}$"
	reg := regexp.MustCompile(str)
	if reg.FindString(username) == "" {
		return false
	}
	return true
}

//registerPassword 注册校验密码规则
func registerPassword(fl validator.FieldLevel) bool {
	//密码是8到20位，连续数组、字母组成
	password := fl.Field().Interface().(string)
	return validatePassword(password)
}

//ValidatePassword 根据密码校验规则对密码校验
func validatePassword(password string) bool {
	if len(password) > 20 || len(password) < 8 {
		return false
	}
	str := "^[0-9a-zA-Z]{8,20}$"
	reg := regexp.MustCompile(str)
	if reg.FindString(password) == "" {
		return false
	}
	return true
}
